//
//  NSObject+BPCrash.m
//  BPCrashDemo
//
//  Created by 404 on 2020/5/12.
//  Copyright © 2020 SiYugui. All rights reserved.
//

#import "NSObject+BPCrash.h"
#import "NSObject+BPSwizzle.h"
#import "BPCrashLog.h"

@implementation NSObject (BPCrash)

+ (void)avoidObjectCrashExchangeMethod{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //setValue:forKey:
        [self exchangeClassMethod:[NSObject class] method1:@selector(setValue:forKey:) method2:@selector(__SetValue:forKey:)];
        
        //setValue:forKeyPath:
        [self exchangeClassMethod:[NSObject class] method1:@selector(setValue:forKeyPath:) method2:@selector(__SetValue:forKeyPath:)];
        
        //setValue:forUndefinedKey:
        [self exchangeClassMethod:[NSObject class] method1:@selector(setValue:forUndefinedKey:) method2:@selector(__SetValue:forUndefinedKey:)];
    
        
        //setValuesForKeysWithDictionary:
        [self exchangeClassMethod:[NSObject class] method1:@selector(setValuesForKeysWithDictionary:) method2:@selector(__SetValuesForKeysWithDictionary:)];
    });
}



//=================================================================
//                         setValue:forKey:
//=================================================================
#pragma mark - setValue:forKey:

- (void)__SetValue:(id)value forKey:(NSString *)key {
    @try {
        [self __SetValue:value forKey:key];
    }
    @catch (NSException *exception) {
        NSString *defaultToDo = BPCrashDefaultReturnIgnore;
        [BPCrashLog noteErrorWithException:exception attachedToDo:defaultToDo];
    }
    @finally {
        
    }
}


//=================================================================
//                     setValue:forKeyPath:
//=================================================================
#pragma mark - setValue:forKeyPath:

- (void)__SetValue:(id)value forKeyPath:(NSString *)keyPath {
    @try {
        [self __SetValue:value forKeyPath:keyPath];
    }
    @catch (NSException *exception) {
        NSString *defaultToDo = BPCrashDefaultReturnIgnore;
        [BPCrashLog noteErrorWithException:exception attachedToDo:defaultToDo];
    }
    @finally {
        
    }
}



//=================================================================
//                     setValue:forUndefinedKey:
//=================================================================
#pragma mark - setValue:forUndefinedKey:

- (void)__SetValue:(id)value forUndefinedKey:(NSString *)key {
    @try {
        [self __SetValue:value forUndefinedKey:key];
    }
    @catch (NSException *exception) {
        NSString *defaultToDo = BPCrashDefaultReturnIgnore;
        [BPCrashLog noteErrorWithException:exception attachedToDo:defaultToDo];
    }
    @finally {
        
    }
}


//=================================================================
//                  setValuesForKeysWithDictionary:
//=================================================================
#pragma mark - setValuesForKeysWithDictionary:

- (void)__SetValuesForKeysWithDictionary:(NSDictionary<NSString *,id> *)keyedValues {
    @try {
        [self __SetValuesForKeysWithDictionary:keyedValues];
    }
    @catch (NSException *exception) {
        NSString *defaultToDo = BPCrashDefaultReturnIgnore;
        [BPCrashLog noteErrorWithException:exception attachedToDo:defaultToDo];
    }
    @finally {
        
    }
}


@end
